package com.syqc.monitor.ui.carinfo

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import autodispose2.autoDispose
import com.syqc.comlib.auto.AutoDisposeViewModel
import com.syqc.comlib.utils.AppContext
import com.syqc.comlib.utils.GsonUtil
import com.syqc.entity.BaseTmn
import com.syqc.entity.ComTmn
import com.syqc.entity.Driver
import com.syqc.entity.KeyValue
import com.syqc.entity.OnlyCarInfo
import com.syqc.entity.VideosPass
import com.syqc.monitor.R
import com.syqc.net.ServiceHelper
import com.syqc.utils.Units

class OnlyCarVM : AutoDisposeViewModel() {
    var carId = ""
    private val carInfo = mutableListOf<OnlyCarInfo>()
    private val ownerInfo = mutableListOf<OnlyCarInfo>()
    private val localInfo = mutableListOf<OnlyCarInfo>()

    private val _carLive = MutableLiveData<MutableList<OnlyCarInfo>>()
    val carLive: LiveData<MutableList<OnlyCarInfo>> = _carLive

    //车主信息
    private val _ownerLive = MutableLiveData<MutableList<OnlyCarInfo>>()
    val ownerLive: LiveData<MutableList<OnlyCarInfo>> = _ownerLive

    //定位信息
    private val _locationLive = MutableLiveData<MutableList<OnlyCarInfo>>()
    val locationLive: LiveData<MutableList<OnlyCarInfo>> = _locationLive

    private val _carPhotos = MutableLiveData<MutableList<String>?>()
    val carPhotos: LiveData<MutableList<String>?> = _carPhotos
    var driverInfo = mutableListOf<Driver>()

    var firstPhone = ""
    var mainTmn: ComTmn? = null
    var videoTmn: ComTmn? = null
    var adasTmn: ComTmn? = null

    init {
        initInfo()
    }

    private fun initInfo() {
        val info = AppContext.getAppContext().resources.getStringArray(R.array.car_info)
        info.forEachIndexed { index, s ->
            carInfo.add(OnlyCarInfo(index, s))
        }
        val owner = AppContext.getAppContext().resources.getStringArray(R.array.car_owner)
        owner.forEachIndexed { index, s ->
            ownerInfo.add(OnlyCarInfo(index, s))
        }
        val local = AppContext.getAppContext().resources.getStringArray(R.array.car_local)
        local.forEachIndexed { index, s ->
            localInfo.add(OnlyCarInfo(index, s))
        }
    }

    /**
     * 获取车辆信息
     */
    fun getGprR() {
        ServiceHelper.getRepository().getGpsRInfo("", carId, simple = false, filter = false)
            .autoDispose(this)
            .subscribe({
                it[0].run {
                    localInfo[0].value = time
                    localInfo[1].value = accTime
                    localInfo[2].value = "$speed ${Units.speed},$drctCn | $stateCn"
                    localInfo[3].value = "$mile ${Units.km}"
                    localInfo[4].value = "$preMile ${Units.km}"
                    localInfo[5].value = runStopTime
                    localInfo[6].value = addr ?: ""
                    localInfo[7].value = sgn
                    localInfo[8].value = satl
                    _locationLive.postValue(localInfo)
                }
            }, error)
    }

    fun getCarInfo() {
        toastLoading(true)
        driverInfo.clear()
        mainTmn = null
        videoTmn = null
        adasTmn = null
        ServiceHelper.getRepository().getCarInfo(carId).autoDispose(this).subscribe({
            toastLoading(false)
            val channelNum = if (it.videos.isNullOrEmpty()) {
                0
            } else {
                val entity = GsonUtil.formObject<VideosPass>(it.videos)
                if (entity.vs == null) {
                    0
                } else {
                    val passEntitys = entity.vs.split(",")
                    passEntitys.size
                }
            }
            it.driverInfo?.let { it1 ->
                driverInfo.addAll(it1)
            }
            firstPhone = if (driverInfo.isEmpty()) "" else driverInfo[0].driverPhone ?: ""

            carInfo[0].value = it.plateColor ?: ""
            carInfo[1].value = it.carName
            carInfo[2].value = it.teamName ?: ""
            carInfo[3].value = it.des ?: ""
            carInfo[4].value = it.videoType ?: ""
            carInfo[5].value = channelNum.toString()
            carInfo[6].value = it.serveEDate ?: ""

            ownerInfo[0].value = it.owner ?: ""
            ownerInfo[1].value = it.ownerPhone ?: ""
            ownerInfo[2].value =
                if (driverInfo.isEmpty()) "" else "${driverInfo[0].driverName}  $firstPhone"

            if (it.mainTmn != null) {
                mainTmn = ComTmn(
                    0,
                    it.mainTmn.tmnPicMap?.values?.toMutableList(),
                    getTmnInfo(it.mainTmn)
                )
            }
            if (it.videoTmn != null) {
                videoTmn = ComTmn(
                    1,
                    it.videoTmn.videoTmnPicMap?.values?.toMutableList(),
                    getTmnInfo(it.videoTmn)
                )
            }
            if (it.adasTmn != null) {
                adasTmn = ComTmn(
                    2,
                    it.adasTmn.adasTmnPicMap?.values?.toMutableList(),
                    getTmnInfo(it.adasTmn)
                )
            }
            _carLive.postValue(carInfo)
            val values = it.carPicMap?.values
            _carPhotos.postValue(values?.toMutableList())
            _ownerLive.postValue(ownerInfo)
        }, error)
    }

    private val tmnDesc by lazy {
        AppContext.getAppContext().resources.getStringArray(R.array.car_tmn)
    }

    private fun getTmnInfo(tmnInfo: BaseTmn?): MutableList<KeyValue> {
        val tmn = mutableListOf<KeyValue>()
        tmnInfo?.run {
            tmn.add(KeyValue(tmnDesc[0], tmnKey ?: ""))
            tmn.add(KeyValue(tmnDesc[1], tmnNo ?: ""))
            tmn.add(KeyValue(tmnDesc[2], sim ?: ""))
            tmn.add(KeyValue(tmnDesc[3], makerModel ?: ""))
            if (type != null) tmn.add(KeyValue(tmnDesc[7], type))
            tmn.add(KeyValue(tmnDesc[4], simMeal ?: ""))
            tmn.add(KeyValue(tmnDesc[5], simType ?: ""))
            tmn.add(KeyValue(tmnDesc[6], expirTime ?: ""))
        }
        return tmn
    }
}